﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Xml.Linq;
using System.IO;
using System.Xml;
using System.Web;
using System.Runtime.InteropServices;
using DevComponents.DotNetBar;
using DevComponents.DotNetBar.Controls;
using DevComponents.DotNetBar.Metro;
using DKT_Reminder.Properties;


namespace DKT_Reminder
{
    public partial class Main : Form
    {
        public Main()
        {
            InitializeComponent();
        }

        bool Mini;
        Rectangle resolution = Screen.PrimaryScreen.Bounds;

        #region Dragging Form

        public const int WM_NCLBUTTONDOWN = 0xA1;
        public const int HT_CAPTION = 0x2;

        [DllImportAttribute("user32.dll")]
        public static extern int SendMessage(IntPtr hWnd, int Msg, int wParam, int lParam);
        [DllImportAttribute("user32.dll")]
        public static extern bool ReleaseCapture();

        public void GenericMouseDownEvent(Object sender, System.Windows.Forms.MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                ReleaseCapture();
                SendMessage(Handle, WM_NCLBUTTONDOWN, HT_CAPTION, 0);

                //Make the form visible
                if (this.Location.X > (resolution.Width - this.Width))
                    this.Location = new Point(resolution.Width - this.Width, this.Location.Y);
                if (this.Location.X < 0)
                    this.Location = new Point(0, this.Location.Y);
            }
        } 

        #endregion

        #region Weather Forecast API

        string temp;
        string cond;
        string humidity;
        string wind;
        string town;
        string cCode;

        private void getWeather()
        {
            try
            {
                lblFail.Visible = false;
                string query = String.Format("http://weather.yahooapis.com/forecastrss?w=1252431&u=c");
                XmlDocument wD = new XmlDocument();
                wD.Load(query);                

                XmlNamespaceManager manager = new XmlNamespaceManager(wD.NameTable);
                manager.AddNamespace("yweather", "http://xml.weather.yahoo.com/ns/rss/1.0");
                
                XmlNode channel = wD.SelectSingleNode("rss").SelectSingleNode("channel");
                XmlNodeList nodes = wD.SelectNodes("/rss/channel/item/yweather:forecast", manager);
                
                temp = channel.SelectSingleNode("item").SelectSingleNode("yweather:condition", manager).Attributes["temp"].Value;
                cond = channel.SelectSingleNode("item").SelectSingleNode("yweather:condition", manager).Attributes["text"].Value;
                humidity = channel.SelectSingleNode("yweather:atmosphere", manager).Attributes["humidity"].Value;
                wind = channel.SelectSingleNode("yweather:wind", manager).Attributes["speed"].Value;
                town = channel.SelectSingleNode("yweather:location", manager).Attributes["city"].Value;
                cCode = channel.SelectSingleNode("item").SelectSingleNode("yweather:condition", manager).Attributes["code"].Value;

                int iPic = int.Parse(cCode);
                lblTemp.Text = temp + " °C";
                lblWind.Text = wind + " km/h";
                lblCond.Text = cond;
                lblCity.Text = town;
                lblHum.Text = humidity + " %";                
                picWeather.Image = weatherImages.Images[iPic];
            }
            catch
            {
                lblFail.Visible = true;
            }
        }

        #endregion

        #region One-line code

        private void lblShow_MouseHover(object sender, EventArgs e)
        {
            lblShow.BackColor = Color.FromArgb(144, 194, 38);
        }

        private void lblShow_MouseLeave(object sender, EventArgs e)
        {
            lblShow.BackColor = Color.FromArgb(84, 160, 33);
        }

        private void wTimer_Tick(object sender, EventArgs e)
        {
            getWeather();
        }

        private void lblLink_Click(object sender, EventArgs e)
        {
            sliLink.SelectedPageIndex = 1;
        }

        private void lblFol_Click(object sender, EventArgs e)
        {
            sliLink.SelectedPageIndex = 2;
        }

        private void lblApp_Click(object sender, EventArgs e)
        {
            sliLink.SelectedPageIndex = 3;
        }

        private void label5_Click(object sender, EventArgs e)
        {
            sliLink.SelectedPageIndex = 0;
        }

        private void lblFail_Click(object sender, EventArgs e)
        {
            getWeather();
        }

        private void lblToday_Click(object sender, EventArgs e)
        {
            sliItem.SelectedPageIndex = 0;
        }

        private void lblUpc_Click(object sender, EventArgs e)
        {
            sliItem.SelectedPageIndex = 1;
        }

        private void lblMiss_Click(object sender, EventArgs e)
        {
            sliItem.SelectedPageIndex = 2;
        }

        private void lblNotes_Click(object sender, EventArgs e)
        {
            sliItem.SelectedPageIndex = 3;
        }

        #endregion

        private void updateDateTime_Tick(object sender, EventArgs e)
        {    
            string s = DateTime.Now.Second.ToString();
            string m = DateTime.Now.Minute.ToString();
            string h = DateTime.Now.Hour.ToString();
            string DoW = DateTime.Now.DayOfWeek.ToString();
            int month = DateTime.Now.Month;
            string MonthStr = "";
            string day = DateTime.Now.Day.ToString();
            string year = DateTime.Now.Year.ToString();
            string Hh;

            if (month == 1) MonthStr = "January";
            if (month == 2) MonthStr = "February";
            if (month == 3) MonthStr = "March";
            if (month == 4) MonthStr = "April";
            if (month == 5) MonthStr = "May";
            if (month == 6) MonthStr = "June";
            if (month == 7) MonthStr = "July";
            if (month == 8) MonthStr = "August";
            if (month == 9) MonthStr = "September";
            if (month == 10) MonthStr = "October";
            if (month == 11) MonthStr = "November";
            if (month == 12) MonthStr = "December";

            if (int.Parse(s) < 10)
                s = "0" + s;
            if (int.Parse(m) < 10)
                m = "0" + m;
            if (int.Parse(h) < 10 || int.Parse(h) > 12 || int.Parse(h) < 22)
                h = "  " + h;

            //12-hours format
            if (int.Parse(h) > 12)
                Hh = (int.Parse(h) - 12).ToString();
            else
                Hh = int.Parse(h).ToString();

            //AM | PM
            if (int.Parse(h) >= 0 && int.Parse(h) < 12)
            {
                lblPM.ForeColor = Color.FromArgb(46, 46, 46);
                lblAM.ForeColor = Color.White;
            }
            else
            {
                lblAM.ForeColor = Color.FromArgb(46, 46, 46);
                lblPM.ForeColor = Color.White;
            }

            if (int.Parse(Hh) < 10) Hh = "  " + Hh;

            lblTime.Text = Hh + ":" + m;
            lblSec.Text = s;
            lblDate.Text = DoW + ", <br />" + MonthStr + " " + day + ", " + year;
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            PictureBox px1 = new PictureBox();
            px1.Height = 16;
            px1.Width = 16;
            notifyIcon1.ShowBalloonTip(10000);
            getWeather();

            //make controls click-through
            this.MouseDown += new MouseEventHandler(GenericMouseDownEvent);
            lblDate.MouseDown += new MouseEventHandler(GenericMouseDownEvent);
            lblTime.MouseDown += new MouseEventHandler(GenericMouseDownEvent);
            lblSec.MouseDown += new MouseEventHandler(GenericMouseDownEvent);
            lblAM.MouseDown += new MouseEventHandler(GenericMouseDownEvent);
            lblPM.MouseDown += new MouseEventHandler(GenericMouseDownEvent);
            line1.MouseDown += new MouseEventHandler(GenericMouseDownEvent);
        }

        private void aboutToolStripMenuItem_Click(object sender, EventArgs e)
        {
            About abt = new About();
            abt.Show();
        }

        private void ẽitToolStripMenuItem_Click(object sender, EventArgs e)
        {
            DialogResult dgl = new DialogResult();
            dgl = MessageBox.Show("Are you sure ?", "DKT Reminder - Exit confirming", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
            if (dgl == DialogResult.Yes)
                Application.Exit();
        }

        private void sliLink_SelectedPageChanged(object sender, EventArgs e)
        {
            int i = sliLink.SelectedPageIndex;
            if (i == 1)
            {
                lblLink.BackColor = Color.FromArgb(84, 160, 33);
                lblFol.BackColor = Color.Transparent;
                lblApp.BackColor = Color.Transparent;
                lblWeather.BackColor = Color.Transparent;
                wTimer.Enabled = false;
            }
            if (i == 2)
            {
                lblLink.BackColor = Color.Transparent;
                lblFol.BackColor = Color.FromArgb(84, 160, 33);
                lblApp.BackColor = Color.Transparent;
                lblWeather.BackColor = Color.Transparent;
                wTimer.Enabled = false;
            }
            if (i == 3)
            {
                lblLink.BackColor = Color.Transparent;
                lblFol.BackColor = Color.Transparent;
                lblApp.BackColor = Color.FromArgb(84, 160, 33);
                lblWeather.BackColor = Color.Transparent;
                wTimer.Enabled = false;
            }
            if (i == 0)
            {
                lblLink.BackColor = Color.Transparent;
                lblFol.BackColor = Color.Transparent;
                lblApp.BackColor = Color.Transparent;
                lblWeather.BackColor = Color.FromArgb(84, 160, 33);
                getWeather();
                wTimer.Enabled = true;
            }
        }

        private void sliItem_SelectedPageChanged(object sender, EventArgs e)
        {
            if (sliItem.SelectedPageIndex == 0)
            {
                lblToday.BackColor = Color.FromArgb(84, 160, 33);
                lblUpc.BackColor = Color.Transparent;
                lblMiss.BackColor = Color.Transparent;
                lblNotes.BackColor = Color.Transparent;
            }
            if (sliItem.SelectedPageIndex == 1)
            {
                lblToday.BackColor = Color.Transparent;
                lblUpc.BackColor = Color.FromArgb(84, 160, 33);
                lblMiss.BackColor = Color.Transparent;
                lblNotes.BackColor = Color.Transparent;
            }
            if (sliItem.SelectedPageIndex == 2)
            {
                lblToday.BackColor = Color.Transparent;
                lblUpc.BackColor = Color.Transparent;
                lblMiss.BackColor = Color.FromArgb(84, 160, 33);
                lblNotes.BackColor = Color.Transparent;
            }
            if (sliItem.SelectedPageIndex == 3)
            {
                lblToday.BackColor = Color.Transparent;
                lblUpc.BackColor = Color.Transparent;
                lblMiss.BackColor = Color.Transparent;
                lblNotes.BackColor = Color.FromArgb(84, 160, 33);
            }
        }

        private void lblShow_Click(object sender, EventArgs e)
        {
            if (this.Height == 122)
                Mini = true;
            if (this.Height == 612)
                Mini = false;
            frmResize.Enabled = true;
            this.Enabled = false;
        }

        private void frmResize_Tick(object sender, EventArgs e)
        {
            if (Mini == true)
            {
                if (this.Height < 532)
                    this.Height += 10;
                if (this.Height < 598)
                    this.Height += 5;
                if (this.Height < 612)
                    this.Height += 1;
                if (this.Height == 612)
                {
                    frmResize.Enabled = false;
                    this.Enabled = true;
                    toolTip1.SetToolTip(lblShow, "Switch to Mini mode");
                }
            }
            else
            {
                if (this.Height > 202)
                    this.Height -= 10;
                if (this.Height > 142)
                    this.Height -= 5;
                if (this.Height > 122)
                    this.Height -= 1;
                if (this.Height == 122)
                {
                    frmResize.Enabled = false;
                    this.Enabled = true;
                    toolTip1.SetToolTip(lblShow, "Switch to Full mode");
                }
            }
        }

        private void txtSearchNote_ButtonCustomClick(object sender, EventArgs e)
        {
            touchKeyboard1.ShowKeyboard(txtSearchNote, DevComponents.DotNetBar.Keyboard.TouchKeyboardStyle.Floating);
            touchKeyboard1.FloatingLocation = new Point((resolution.Width / 2) - (touchKeyboard1.FloatingSize.Width / 2), resolution.Height - touchKeyboard1.FloatingSize.Height - 30);
            txtSearchNote.Select();
        }
    }
}
